/**
 * Copyright 2014,Peak Tai 台俊峰(taijunfeng_it@sina.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cenluan.activity;

import java.util.Date;

import com.cenluan.activity.comment.ActivityComment;
import com.cenluan.common.Const;
import com.cenluan.ext.jfinal.PkController;
import com.cenluan.kit.UUIDKit;
import com.cenluan.kit.XssKit;
import com.cenluan.login.ClearLoginInterceptor;
import com.cenluan.me.message.Message;
import com.cenluan.user.User;
import com.cenluan.user.log.UserLog;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;

/**
 * 活动
 * 
 * @author peak
 * 
 */
public class ActivityController extends PkController {
	/**
	 * 列表
	 * 
	 */
	@ClearLoginInterceptor
	public void index() {
		User user = getSessionAttr(Const.SESSION_USER);
		String createrId = user == null ? null : user.getStr("id");
		Page<Activity> page = ActivityService.paginate(getPn(),
				getPara("show", "new"), createrId);
		setPage(page);
		keepPara();

	}

	/**
	 * 添加
	 */
	public void add() {
	}

	/**
	 * 保存
	 */
	@Before(ActivitySaveValidator.class)
	public void save() {
		User user = getSessionAttr(Const.SESSION_USER);
		Date lastTime = ActivityService.getUserLastSaveAvtivityTime(user
				.getStr("id"));
		if (lastTime != null
				&& (lastTime.getTime() + 1000L * 60L * 30L) > new Date()
						.getTime()) {
			setAttr("msg", "您发起活动过于频繁，请过段时间再来尝试此操作");
			renderJson();
			return;
		}
		Activity activity = getModel(Activity.class);
		activity.set("id", UUIDKit.generateUUID());
		activity.set("createTime", new Date());
		activity.set("createrId", user.getStr("id"));
		activity.set("state", Activity.STATE_ACTIVE);
		activity.save();
		setAttr("ok", true);
		renderJson();
		// 动态
		UserLog.save(user.getStr("id"), UserLog.TYPE_ACTIVITY,
				activity.getStr("id"));
	}

	/**
	 * 编辑
	 */
	public void edit() {
		User user = getSessionAttr(Const.SESSION_USER);
		Activity activity = Activity.dao.findById(getPara());
		if (!user.getStr("id").equals(activity.getStr("createrId"))) {
			renderError(403);
		}
		setAttr("activity", activity);
	}

	/**
	 * 更新
	 */
	@Before(ActivityUpdateValidator.class)
	public void update() {
		User user = getSessionAttr(Const.SESSION_USER);
		Activity activity = getModel(Activity.class);
		Activity old = Activity.dao.findById(activity.getStr("id"));
		if (!old.getStr("createrId").equals(user.getStr("id"))) {
			renderError(403);
		}
		// 判断是否已经到了开始时间，到了就不能再改时间
		if (old.getDate("start").before(new Date())) {
			activity.keep("id", "title", "content");
		} else {
			if (activity.getDate("start") == null
					|| activity.getDate("end") == null) {
				setAttr("msg", "数据异常，请刷新页面重新填写表单");
				renderJson();
				return;
			}
			activity.keep("id", "title", "content", "start", "end");
		}

		activity.update();
		setAttr("ok", true);
		renderJson();
	}

	/**
	 * 删除
	 */
	public void delete() {
		User user = getSessionAttr(Const.SESSION_USER);
		Activity activity = Activity.dao.findById(getPara());
		if (!user.isAdmin()
				&& !activity.getStr("createrId").equals(user.getStr("id"))) {
			renderError(403);
		}
		activity.set("state", Activity.STATE_DELETE);
		// 删除通知
		if (!activity.getStr("createrId").equals(user.getStr("id"))) {
			Message.save(user.getStr("id"), activity.getStr("createrId"),
					"将活动\"" + XssKit.escapeStr(activity.getStr("title"))
							+ "\"删除");
		}
		activity.keep("id", "state").update();
		setAttr("ok", true);
		renderJson();
	}

	/**
	 * 展示
	 */
	public void show() {
		User user = getSessionAttr(Const.SESSION_USER);
		Activity activity = Activity.dao.findById(getPara());
		if (activity == null) {
			renderError(404);
		}
		if (!Activity.STATE_ACTIVE.equals(activity.getStr("state"))) {
			renderError(404);
		}
		boolean isActivityCreater = activity.getStr("createrId").equals(
				user.getStr("id"));

		if (user.isAdmin() || isActivityCreater) {
			setAttr("deletable", true);
		} else {
			setAttr("deletable", false);
		}

		if (isActivityCreater) {
			setAttr("editable", true);
		} else {
			setAttr("editable", false);
		}

		Page<ActivityComment> page = ActivityService.paginateComment(getPn(),
				getPara());
		for (ActivityComment comment : page.getList()) {
			if (user.isAdmin() || isActivityCreater
					|| comment.getStr("createrId").equals(user.getStr("id"))) {
				comment.put("deletable", true);
			} else {
				comment.put("deletable", false);
			}
		}

		setAttr("activity", activity);
		setPage(page);

	}

}
